package com.gewu.pm.ui.adapter;

import android.content.Context;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.gewu.pm.R;
import com.gewu.pm.app.AppAdapter;
import com.gewu.pm.bean.FunctionModuleBean;

/**
 * 功能模块适配器
 */
public final class FunctionModuleAdapter extends AppAdapter<FunctionModuleBean> {

    private FunctionItemAdapter.OnAddClickListener mAddListener;
    private FunctionItemAdapter.OnItemClickListener mItemClickListener;
    private boolean isEditMode = true; // 默认为编辑模式

    public FunctionModuleAdapter(Context context) {
        super(context);
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        return new ModuleHolder();
    }

    public void setOnAddClickListener(FunctionItemAdapter.OnAddClickListener listener) {
        this.mAddListener = listener;
    }
    
    public void setOnItemClickListener(FunctionItemAdapter.OnItemClickListener listener) {
        this.mItemClickListener = listener;
    }
    
    /**
     * 设置是否为编辑模式
     * @param isEditMode true-编辑模式，false-查看模式
     */
    public void setEditMode(boolean isEditMode) {
        this.isEditMode = isEditMode;
    }

    private final class ModuleHolder extends AppAdapter<?>.ViewHolder {

        private final TextView tvModuleName;
        private final RecyclerView rvFunctions;
        private final FunctionItemAdapter adapter;

        private ModuleHolder() {
            super(R.layout.function_module_item);
            tvModuleName = findViewById(R.id.tv_module_name);
            rvFunctions = findViewById(R.id.rv_functions);

            // 设置网格布局，每行4个
            GridLayoutManager layoutManager = new GridLayoutManager(getContext(), 4);
            rvFunctions.setLayoutManager(layoutManager);

            adapter = new FunctionItemAdapter(getContext());
            rvFunctions.setAdapter(adapter);
        }

        @Override
        public void onBindView(int position) {
            FunctionModuleBean module = getItem(position);
            tvModuleName.setText(module.getModuleName());
            adapter.setData(module.getFunctionList());
            adapter.setEditMode(isEditMode);
            adapter.setOnAddClickListener(mAddListener);
            adapter.setOnItemClickListener(mItemClickListener);
        }
    }
}

